package com.guofen.controller.sjq;

import com.guofen.pojo.SjqWeightProductionFaecesInfo;
import com.guofen.service.sjq.SjqWeightProductionFaecesInfoService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RequestMapping("/sjq/weightProductionFaecesInfo")
@Controller
@Api(tags = "SjqWeightProductionFaecesInfoController", description = "称重数据导入表-粪便")
public class SjqWeightProductionFaecesInfoController {

    @Autowired
    SjqWeightProductionFaecesInfoService sjqWeightProductionFaecesInfoService;

    @ApiOperation("EXCEL批量添加记录")
    @PostMapping("/addFromFaecesExcel")
    @ResponseBody
    public Result doRegisterFromExcel1(@RequestParam("file") MultipartFile file){
        String EXCEL_XLS = "xls";
        String EXCEL_XLSX = "xlsx";

        if (file.isEmpty())
            return ResultGenerator.genFailResult("文件为空");
        List<Boolean> registerResult = new ArrayList<>();

        try {
            //根据路径获取这个操作excel的实例
            Sheet sheet = null;
            Row row = null;
            if (file.getOriginalFilename().endsWith(EXCEL_XLS)) {
                HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
                sheet = wb.getSheetAt(0);
            } else if (file.getOriginalFilename().endsWith(EXCEL_XLSX)) {
                XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
                //根据页面index 获取sheet页
                sheet = wb.getSheetAt(0);
            }

            List<SjqWeightProductionFaecesInfo> sjqWeightProductionFaecesInfoList= new ArrayList<>();

            for (int i = 2; i < Objects.requireNonNull(sheet).getPhysicalNumberOfRows(); i++) {
                //获取每一行数据
                row = sheet.getRow(i);
                if(row.getCell(0)==null && row.getCell(1)==null && row.getCell(2)==null && row.getCell(3)==null
                        && row.getCell(4)==null && row.getCell(5)==null && row.getCell(6)==null
                        && row.getCell(7)==null && row.getCell(8)==null){
                    registerResult.add(false);
                    continue;
                }
                SjqWeightProductionFaecesInfo sjqWeightProductionFaecesInfo = new SjqWeightProductionFaecesInfo();
                Cell one = row.getCell(0);
                one.setCellType(CellType.STRING);
                if(!one.getStringCellValue().equals("合计") && !one.getStringCellValue().equals("总合计")){
                    if (row.getCell(1) != null){
                        Cell cell = row.getCell(1);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionFaecesInfo.setLicensePlate(cell.getStringCellValue());
                    }
                    if (row.getCell(2) != null){
                        Cell cell = row.getCell(2);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionFaecesInfo.setCarryCompany(cell.getStringCellValue());
                    }
                    if (row.getCell(3) != null){
                        Cell cell = row.getCell(3);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionFaecesInfo.setGarbageType(cell.getStringCellValue());
                    }
                    if (row.getCell(4) != null){
                        Cell cell = row.getCell(4);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionFaecesInfo.setCarDoorNumber(cell.getStringCellValue());
                    }
                    if (row.getCell(5) != null){
                        Cell cell = row.getCell(5);
                        cell.setCellType(CellType.NUMERIC);
                        sjqWeightProductionFaecesInfo.setGrossWeight(cell.getNumericCellValue());
                    }
                    if (row.getCell(6) != null){
                        Cell cell = row.getCell(6);
                        cell.setCellType(CellType.NUMERIC);
                        sjqWeightProductionFaecesInfo.setTareWeight(cell.getNumericCellValue());
                    }
                    if (row.getCell(7) != null){
                        Cell cell = row.getCell(7);
                        cell.setCellType(CellType.NUMERIC);
                        sjqWeightProductionFaecesInfo.setNetWeight(cell.getNumericCellValue());
                    }
                    if (row.getCell(8) != null){
                        Cell cell = row.getCell(8);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionFaecesInfo.setNetWeightTime(cell.getStringCellValue());
                    }
                    sjqWeightProductionFaecesInfoList.add(sjqWeightProductionFaecesInfo);
                }
            }
            registerResult = sjqWeightProductionFaecesInfoService.addFromFaecesExcel(sjqWeightProductionFaecesInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultGenerator.genSuccessResult(registerResult);
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(sjqWeightProductionFaecesInfoService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="licensePlate",value="车牌号码",required=true) @RequestParam String licensePlate,
                         @ApiParam(name="carryCompany",value="承运单位",required=true) @RequestParam String carryCompany,
                         @ApiParam(name="garbageType",value="垃圾类型",required=true) @RequestParam String garbageType,
                         @ApiParam(name="carDoorNumber",value="车门号",required=true) @RequestParam String carDoorNumber,
                         @ApiParam(name="grossWeight",value="毛重(KG)",required=true) @RequestParam Double grossWeight,
                         @ApiParam(name="tareWeight",value="皮重(KG)",required=true) @RequestParam Double tareWeight,
                         @ApiParam(name="netWeight",value="净重(KG)",required=true) @RequestParam Double netWeight,
                         @ApiParam(name="netWeightTime",value="净重时间",required=true) @RequestParam String netWeightTime) {
        SjqWeightProductionFaecesInfo record = new SjqWeightProductionFaecesInfo();
        record.setId(id);
        record.setLicensePlate(licensePlate);
        record.setCarryCompany(carryCompany);
        record.setGarbageType(garbageType);
        record.setCarDoorNumber(carDoorNumber);
        record.setGrossWeight(grossWeight);
        record.setTareWeight(tareWeight);
        record.setNetWeight(netWeight);
        record.setNetWeightTime(netWeightTime);
        return ResultGenerator.genSuccessResult(sjqWeightProductionFaecesInfoService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(sjqWeightProductionFaecesInfoService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(sjqWeightProductionFaecesInfoService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
